<template>
  <div class="attr-info">
    <el-dialog
      :title="info.type === 'add' ? '新增角色' : '编辑角色'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="属性名" prop="specsname">
          <el-input v-model="ruleForm.specsname"></el-input>
        </el-form-item>
        <el-form-item label="属性值">
          <div v-for="(item, index) in attrList" :key="index">
            <el-input class="attr-input" v-model="attrList[index]"></el-input>
            <el-button v-if="index" type="danger" @click="del(index)"
              >删除属性</el-button
            >
            <el-button v-else type="success" @click="add">新增属性</el-button>
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="info.type === 'add'"
            type="primary"
            @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button v-else type="success" @click="submitForm('ruleForm')"
            >立即修改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { specsAdd } from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      // 属性值对应的数组
      attrList: [""],
      ruleForm: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 给表单赋值
    setValue(info) {
      // 深拷贝，比避免数据互相影响
      this.ruleForm = { ...info };
      this.attrList = [...this.ruleForm.attrs];
    },
    // 增加一项
    add() {
      if (this.attrList.length < 5) {
        this.attrList.push("");
      } else {
        this.$message({
          type: "warning",
          message: '属性值最多5个',
        });
      }
    },
    // 删除一项
    del(index) {
      this.attrList.splice(index, 1);
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let arr = [...this.attrList];
          this.ruleForm.attrs = arr.join(",");
          if (this.info.type === "add") {
            let res = await specsAdd(this.ruleForm);
            console.log(res);
          } else {
            // 编辑
            let res = await roleEdit(this.ruleForm);
          }
          // 关闭弹窗
          this.info.isShow = false;
          // 通知list再次获取数据  info->index->list
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.attr-info .attr-input {
  width: 200px;
  margin-right: 20px;
  margin-bottom: 10px;
}
</style>